package com.yc.rh.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.yc.rh.dao.INewartInfoDao;
import com.yc.rh.entities.NewartInfo;

public class NewartInfoDaoImpl implements INewartInfoDao{
	private SqlSession sqlSession;
	
	public void setSqlSession(SqlSession sqlSession) {
		this.sqlSession = sqlSession;
	}
	
	@Override
	public int addNewart(NewartInfo ni) {
		return sqlSession.insert("addNewart", ni);
	}

	@Override
	public List<NewartInfo> find(Integer pageNo, Integer pageSize) {
		Map<String,Object> map=new HashMap<String,Object>();
		if(pageNo!=null && pageSize!=null){
			map.put("min", (pageNo-1)*pageSize);
			map.put("max", pageNo*pageSize);
		}else{
			map.put("min", null);
			map.put("max", null);
		}
		return sqlSession.selectList("findNewartAll",map);
	}

	@Override
	public int delNews(String naids) {
		List<Integer> list=new ArrayList<Integer>();
		while(naids.contains(",")){
			Integer naid=Integer.valueOf(naids.substring(0, naids.indexOf(",")));
			naids=naids.substring(naids.indexOf(",")+1);
			list.add(naid);
		}
		list.add(Integer.valueOf(naids));
		return sqlSession.delete("delNewart",list);
	}

	@Override
	public int getTotal() {
		return sqlSession.selectOne("getNewartTotal");
	}

	@Override
	public int updatePic(NewartInfo ni) {
		return sqlSession.update("updateNewart",ni);
	}

	@Override
	public int getNaid(String naname) {
		return sqlSession.selectOne("findNaidByName", naname);
	}

	@Override
	public String findNaContentImgByNaid(int naid) {
		return sqlSession.selectOne("findImgByNaid", naid);
	}

	@Override
	public List<NewartInfo> findNewartInfoByDate(NewartInfo ni, Integer pageNo, Integer pageSize) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("natime", ni.getNatime());
		map.put("naname", ni.getNaname());
		map.put("min", (pageNo-1)*pageSize);
		map.put("max", pageNo*pageSize);
		return sqlSession.selectList("findNewartByDate", map);
	}

	@Override
	public int getTotal(NewartInfo ni) {
		return sqlSession.selectOne("getNewartTotal",ni);
	}
}
